package io.renren.modules.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.TiananDao;
import io.renren.modules.sys.dao.TiananSubmitDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.*;
import io.renren.modules.sys.util.DateUtil;
import io.renren.modules.sys.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.*;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;


@Service("tiananService")
public class TiananServiceImpl extends ServiceImpl<TiananDao, TiananEntity> implements TiananService {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysDeptService sysDeptService;
    @Autowired
    SysDictService sysDictService;
    @Autowired
    TiananFuyiService tiananFuyiService;
    @Autowired
    TiananSubmitDao tiananSubmitService;
    @Autowired
    XuanquService xuanquService;
    @Autowired
    TiananDao tiananDao;


    private QueryWrapper<TiananEntity> getWrapper(QueryWrapper<TiananEntity> wrapper) {


        wrapper.eq("del_flag", Constant.DEL_FLAG);
        wrapper.orderByDesc("id");
        return wrapper;
    }


    @Override
    public List<TiananEntity> lists() {
        return getName(tiananDao.selectList(new QueryWrapper<TiananEntity>()
                .orderByDesc("id")
                .eq("yushenstatus", "1")
                .eq("year", DateUtil.getCurrentYear())

        ), false, false);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        String banjiestatus = "" + params.get("banjiestatus");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                new QueryWrapper<TiananEntity>()
                        .eq("creatuser", sysUserEntity.getUserId())
                        .eq("year", DateUtil.getCurrentYear())
                        .eq(StringUtil.isNotEmpty(banjiestatus), "banjiestatus", banjiestatus)
             .orderByDesc("id")
        );
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagefuyi(Map<String, Object> params) {
        String userId = "" + params.get("userId");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year", DateUtil.getCurrentYear())
                        .apply("find_in_set(" + userId + ",fuyiren)"))
        );
        getName(page.getRecords(), true, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagegongshi(Map<String, Object> params) {
        String sea = "" + params.get("sea");
        String types = "" + params.get("types");
        String manyi = "" + params.get("manyi");
        String liandept = "" + params.get("liandept");
        String year = "" + params.get("year");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq(StringUtil.isNotEmpty(year),"year",year)
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(types), "types", types)
                        .eq("gongshi", 1)
                        .eq(StringUtil.isNotEmpty(manyi), "manyi", manyi)
                        .eq(StringUtil.isNotEmpty(liandept), "liandept", liandept)
                ));
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagegongshichuli(Map<String, Object> params) {
        String sea = "" + params.get("sea");
        String types = "" + params.get("types");
        String manyi = "" + params.get("manyi");
        String liandept = "" + params.get("liandept");
        String tongyi = "" + params.get("tongyi");
        String gongshi = "" + params.get("gongshi");
        String year = "" + params.get("year");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year",year)
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(types), "types", types)
                        .eq(StringUtil.isNotEmpty(gongshi), "gongshi", gongshi)
                        .eq("banjiestatus", 6)
                        .eq(StringUtil.isNotEmpty(manyi), "manyi", manyi)
                        .isNotNull("tongyi")
                        .eq(StringUtil.isNotEmpty(liandept), "liandept", liandept)
                        .eq(StringUtil.isNotEmpty(tongyi), "tongyi", tongyi)
                ));
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagejilu(Map<String, Object> params) {
        String sea = "" + params.get("sea");
        String types = "" + params.get("types");
        String year = "" + params.get("year");
        String manyi = "" + params.get("manyi");
        String liandept = "" + params.get("liandept");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(types), "types", types)
                        .eq(StringUtil.isNotEmpty(year), "year", year)
                        .eq(StringUtil.isNotEmpty(manyi), "manyi", manyi)
                        .eq(StringUtil.isNotEmpty(liandept), "liandept", liandept)
                ));
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageqianpi(Map<String, Object> params) {
        String sea = "" + params.get("sea");
        String types = "" + params.get("types");
        String manyi = "" + params.get("manyi");
        String tongyi = "" + params.get("tongyi");
        String liandept = "" + params.get("liandept");
        String year = "" + params.get("year");

        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                new QueryWrapper<TiananEntity>()
                        .eq("year", year)
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(types), "types", types)
                        .eq("banjiestatus", 6)
                        .eq(StringUtil.isNotEmpty(manyi), "manyi", manyi)
                        .isNotNull(StringUtil.isNotEmpty(tongyi) && tongyi.equals("1"), "tongyi")
                        .isNull(StringUtil.isNotEmpty(tongyi) && tongyi.equals("0"), "tongyi")
                        .eq(StringUtil.isNotEmpty(liandept), "liandept", liandept)
                        .orderByDesc("id")
                        .eq("del_flag", Constant.DEL_FLAG)
                        .orderByAsc("lable")
                        .orderByAsc("banjiestatus")

        );
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagelianming(Map<String, Object> params) {
        String userId = "" + params.get("userId");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year", DateUtil.getCurrentYear())
                        .eq("lianming", userId))
        );
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageyushen(Map<String, Object> params) {
        String yushenstatus = "" + params.get("yushenstatus");
        String sea = "" + params.get("sea");
        IPage<TiananEntity> page = null;

        if (StringUtil.isNotEmpty(yushenstatus)) {


            page = this.page(
                    new Query<TiananEntity>().getPage(params),
                    getWrapper(new QueryWrapper<TiananEntity>()
                            .eq("year", DateUtil.getCurrentYear())
                            .like(StringUtil.isNotEmpty(sea), "title", sea)
                            .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                            .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                            .eq(StringUtil.isNotEmpty(yushenstatus), "yushenstatus", yushenstatus)
                    )
            );
        } else {
            page = this.page(
                    new Query<TiananEntity>().getPage(params),
                    getWrapper(new QueryWrapper<TiananEntity>()
                            .eq("year", DateUtil.getCurrentYear())
                            .like(StringUtil.isNotEmpty(sea), "title", sea)
                            .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                            .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                            .apply("(yushenstatus=1 or yushenstatus=0)")
                    )
            );
        }
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPagelian(Map<String, Object> params) {
        String lianstatus = "" + params.get("lianstatus");
        String sea = "" + params.get("sea");
        String liantype = "" + params.get("liantype");
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year", DateUtil.getCurrentYear())
                        .apply("yushenstatus=1")
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(lianstatus), "lianstatus", lianstatus)
                        .eq(StringUtil.isNotEmpty(liantype), "liantype", liantype)


                ));
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }
    @Override
    public PageUtils queryPagexuanqu(Map<String, Object> params) {
        String sea = "" + params.get("sea");
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year", DateUtil.getCurrentYear())
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq("xuanqu", sysUserEntity.getXuanqu())
                        .apply("(yushenstatus=1 or yushenstatus=0)")

                ));
        getName(page.getRecords(), false, false);
        return new PageUtils(page);
    }



    @Override
    public PageUtils queryPagechuli(Map<String, Object> params) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        SysDeptEntity sysDeptEntity = sysDeptService.getById(sysUserEntity.getDeptId());

        String banjiestatus = "" + params.get("banjiestatus");
        String sea = "" + params.get("sea");
        IPage<TiananEntity> page = null;
        if (sysDeptEntity.getResuser() != null && sysDeptEntity.getResuser().equals("" + sysUserEntity.getUserId())) {
            if (StringUtil.isEmpty(banjiestatus)) {
                page = this.page(
                        new Query<TiananEntity>().getPage(params),
                        getWrapper(new QueryWrapper<TiananEntity>()
                                .eq("year", DateUtil.getCurrentYear())
                                .like(StringUtil.isNotEmpty(sea), "title", sea)
                                .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                                .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                                .eq("liandept", sysUserEntity.getDeptId())
                                .apply("(lianstatus =1 or lianstatus =2 or banjiestatus=3 or lianstatus=3)")
                                .apply("liantype!=3")
                        )
                );
            } else {
                page = this.page(
                        new Query<TiananEntity>().getPage(params),
                        getWrapper(new QueryWrapper<TiananEntity>()
                                .eq("year", DateUtil.getCurrentYear())
                                .like(StringUtil.isNotEmpty(sea), "title", sea)
                                .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                                .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                                .eq("liandept", sysUserEntity.getDeptId())
                                .eq("banjiestatus", banjiestatus)

                        )
                );
            }


            getName(page.getRecords(), false, true);
            return new PageUtils(page);
        } else {
            return null;
        }
    }

    @Override
    public PageUtils queryPagechulis(Map<String, Object> params) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        SysDeptEntity sysDeptEntity = sysDeptService.getById(sysUserEntity.getDeptId());
        IPage<TiananEntity> page = null;
        if (sysDeptEntity.getResuser() != null && sysDeptEntity.getResuser().equals("" + sysUserEntity.getUserId())) {
            page = this.page(
                    new Query<TiananEntity>().getPage(params),
                    getWrapper(new QueryWrapper<TiananEntity>()
                                    .eq("year", DateUtil.getCurrentYear())
                                    .eq("liandept", sysUserEntity.getDeptId())
                                    .apply("(banjiestatus=3 or lianstatus=1)")
                                    .apply("liantype!=3")
//                            .apply(" order by (case when banjiestatus=3 then 1 else lianstatus=3 END),id desc;")
                    )
            );


            getName(page.getRecords(), false, true);
            return new PageUtils(page);
        } else {
            return null;
        }
    }

    @Override
    public PageUtils queryPagebanli(Map<String, Object> params) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        String banjiestatus = "" + params.get("banjiestatus");
        String sea = "" + params.get("sea");

        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .eq("year", DateUtil.getCurrentYear())
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq(StringUtil.isNotEmpty(banjiestatus), "banjiestatus", banjiestatus)
                        .eq("jingbanuser", sysUserEntity.getUserId())

                )
        );
        getName(page.getRecords(), false, false);
        return new PageUtils(page);

    }

    @Override
    public PageUtils queryPageshenpi(Map<String, Object> params) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        List<Object> list = sysDeptService.listObjs(new QueryWrapper<SysDeptEntity>()
                .select("dept_id")
                .eq("leauser", sysUserEntity.getUserId())

        );
        String banjiestatus = "" + params.get("banjiestatus");
        String sea = "" + params.get("sea");

        IPage<TiananEntity> page = this.page(
                new Query<TiananEntity>().getPage(params),
                getWrapper(new QueryWrapper<TiananEntity>()
                        .in( list.size()>0,"liandept", list)
                        .eq("year", DateUtil.getCurrentYear())
                        .like(StringUtil.isNotEmpty(sea), "title", sea)
                        .like(StringUtil.isNotEmpty(sea), "anyoufenxi", sea)
                        .like(StringUtil.isNotEmpty(sea), "cuoshijianyi", sea)
                        .eq("banjiestatus", 4)


                )
        );


        getName(page.getRecords(), false, true);
        return new PageUtils(page);

    }

    @Override
    public TiananEntity getid(String id) {

        return getnames(tiananDao.selectById(id), false, true);
    }

    @Override
    public Float over(String year) {
        return tiananDao.over(year);
    }

    @Override
    public Float overtime(String year) {
        return tiananDao.overtime(year);
    }

    @Override
    public List<Map<String, String>> manyi(String year) {
        return tiananDao.manyi(year);
    }

    @Override
    public List<Map<String, String>> type(String year) {
        return tiananDao.type(year);
    }

    @Override
    public List<Map<String, String>> types(String year) {
        return tiananDao.types(year);
    }

    @Override
    public List<Map<String, String>> liantype(String year) {
        return tiananDao.liantype(year);
    }

    @Override
    public List<Map<String, String>> liandept(String year) {
        return tiananDao.liandept(year);
    }

    @Override
    public List<Map<String, String>> lianren(String year) {
        return tiananDao.lianren(year);
    }

    @Override
    public void updateendtime() {
        tiananDao.updateendtime(new java.util.Date(), DateUtil.getCurrentYear());
    }

    @Override
    public List<String> listyear() {
        return tiananDao.listyear();
    }

    @Override
    public List<SysUserEntity> listreuser() {
        return tiananDao.listreuser();
    }

    @Override
    public List<SysUserEntity> listleauser() {
        return tiananDao.listleauser();
    }

    public List<TiananEntity> getName(List<TiananEntity> entities, boolean fuyi, boolean check) {
        //获取实际名称
        entities.forEach(e -> {
            //附议人名称
            getnames(e, fuyi, check);
        });
        return entities;
    }


    public TiananEntity getnames(TiananEntity e, boolean fuyi, boolean check) {

        //联名人名称
        SysUserEntity userEntity1 = sysUserService.getById(e.getLianming());
        if (userEntity1 != null) {
            e.setLianmingName(userEntity1.getRealname());
        }
        //类型名
        SysDictEntity dictEntity = sysDictService.getById(e.getTypes());
        if (dictEntity != null) {
            e.setTypesName(dictEntity.getValue());
        }
        //创建人
        SysUserEntity userEntity2 = sysUserService.getById(e.getCreatuser());
        if (userEntity2 != null) {
            e.setCreatusername(userEntity2.getRealname());
        }
        //部门名称
        SysDeptEntity sysDeptEntity = sysDeptService.getById(e.getDeptId());
        if (sysDeptEntity != null) {
            e.setDeptIdName(sysDeptEntity.getName());
        }
       XuanquEntity entity = xuanquService.getById(e.getXuanqu());
        if (entity != null) {
            e.setXuanquname(entity.getName());
        }
        //立案部门
        SysDeptEntity sysDeptEntity1 = sysDeptService.getById(e.getLiandept());
        if (sysDeptEntity1 != null) {
            e.setLiandeptname(sysDeptEntity1.getName());
        }
        //经办人
        userEntity2 = sysUserService.getById(e.getJingbanuser());
        if (userEntity2 != null) {
            e.setJingbanusername(userEntity2.getRealname());
        } else {
            e.setJingbanusername("未设置");

        }
        if (fuyi) {
            SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
            Map<String, Object> params = new HashMap<>();
            params.put("tianid", "" + e.getId());
            params.put("userid", "" + sysUserEntity.getUserId());
            TiananFuyiEntity tiananFuyiEntity = tiananFuyiService.selectone(params);
            if (tiananFuyiEntity != null) {
                e.setFuid("" + tiananFuyiEntity.getId());
                e.setFustatus("" + tiananFuyiEntity.getStatus());
            }
        }
        if (check) {
            List<TiananSubmitEntity> list = tiananSubmitService.selectList(new QueryWrapper<TiananSubmitEntity>()
                    .eq("tianid", e.getId())
                    .orderByDesc("id")
            );
            if (list.size() > 0) {

                e.setSubmitid("" + list.get(0).getId());
            }

        }

        return e;
    }


}
